package edu.qichen.onlineBlog.email.service.impl;

import cn.hutool.core.util.StrUtil;
import edu.qichen.onlineBlog.email.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

/**
 * 功能:
 * 作者:程序员欧阳子韩
 * 日期:2024/4/19 16:16
 */
@Service
@Slf4j
public class MaiServiceImpl implements MailService {
    @Value("${spring.mail.username}")
    private String clientMail;

    @Autowired
    private JavaMailSender mailSender;

    @Override
    public boolean sendMail(String email, String code) {
        if (StrUtil.isEmpty(email)) {
            return false;
        }

        String subject = "邮箱验证码";

        String text = "尊敬的用户，你的校验码:" + code + ",有效期为5分钟，工作人员不会索取，请勿泄露";

        SimpleMailMessage msg = new SimpleMailMessage();

        msg.setFrom(clientMail);
        msg.setTo(email);
        msg.setSubject(subject);
        msg.setText(text);
        try {
            mailSender.send(msg);
            log.info("msg=======>{}", msg);
        } catch (MailException e) {
            e.getMessage();
        }
        return true;
    }
}
